package com.codi.bus.core.service.impl;

import com.codi.bus.constant.GlobalConstant;
import com.codi.bus.core.dao.UserDao;
import com.codi.bus.core.domain.User;
import com.codi.bus.core.service.H5Service;
import com.codi.goldlife.external.IntegralServiceApi;
import com.codi.goldlife.external.databean.ResultBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @Author: zengxl
 * @Description:
 * @Date: 2018/3/12 9:57
 */
@Service
@Slf4j
public class H5ServiceImpl implements H5Service {

    @Resource(name = "userDao")
    private UserDao userDao;
    @Autowired
    private IntegralServiceApi integralServiceApi;

    @Transactional
    @Override
    public User addUserByH5(User entity) {
        User user = userDao.add(entity);
        ResultBean resultBean = integralServiceApi.execIntegral(user.getUserId().toString(), GlobalConstant.INTEGRAL_CHANNEL_ID, GlobalConstant.INTEGRAL_BUSINESSID_REGISTER, user.getUserMobile());
        if (resultBean.isSuccess()) {
            integralServiceApi.sendUserMsg(user.getUserId().toString(), user.getUserMobile(), "", GlobalConstant.INTEGRAL_CHANNEL_ID);
            log.info("H5--用户注册积分添加成功,用户ID={},电话={}", user.getUserId(), user.getUserMobile());
        } else {
            log.error("H5--用户注册积分添加失败,用户ID={},电话={}", user.getUserId(), user.getUserMobile());
        }
        return user;
    }
}
